﻿using System;
using System.Web.UI.WebControls;
using Cms.Application;
using Cms.Application.WebFramework.Presenters;
using Cms.Application.WebFramework.Views;

namespace Cms.Web.Framework.Controls
{
    public class CustomAdminPage : AdminPage, ICustomAdminPageView
    {
        private CustomAdminPagePresenter _presenter;

        #region ICustomAdminPageView Members

        public string Filename
        {
            get 
            {
                string virtualPath = Request.Url.AbsolutePath;
                int lastSlashPosition = virtualPath.LastIndexOf("/");
                return lastSlashPosition >= 0 ? virtualPath.Remove(0, lastSlashPosition + 1) : virtualPath;
            }
        }

        public void SetHeading(string headingText)
        {
            HeadingText = headingText;
        }

        #endregion

        protected string GetTranslation(string key)
        {
            string translationFile = this.Filename.Substring(0, Filename.Length - 5) + ".xml";
            return Translator.GetText(translationFile, key, this.Localization);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new CustomAdminPagePresenter(this);
            _presenter.SetHeading();
            if (Page.IsPostBack)
            {
                return;
            }
            Literal litIcon = (Literal)FindControl("litIcon");
            HeadingIcon = litIcon != null ? litIcon.Text : "/Admin/Img/icons/page_white_visualstudio.png";
        }
    }
}
